package com.zhang.yunfu_his.service.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.bingmei_incomeexpenditure.entity.commom.R;
import com.zhang.yunfu_his.entity.dbo.InquiryDoctorAdviceDO;
import com.zhang.yunfu_his.entity.req.PatientPrescriptionListREQ;
import com.zhang.yunfu_his.entity.ret.PatientPrescriptionVO;
import com.zhang.yunfu_his.mapper.InquiryPrescriptionMapper;
import com.zhang.yunfu_his.service.PrescriptionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author zsj
 * @description
 * @createDate 2025/11/21 19:13
 * @Entity com.zhang.yunfu_his.service.Impl.entity.
 */
@Service
@Slf4j
public class PrescriptionServiceImpl implements PrescriptionService {

    @Resource
    private InquiryPrescriptionMapper prescriptionMapper;

    @Override
    public R<List<InquiryDoctorAdviceDO>> getInquiryDoctorAdviceList(PatientPrescriptionListREQ req) {
        return R.ok(prescriptionMapper.getInquiryDoctorAdviceList(req));
    }

    @Override
    public R<List<PatientPrescriptionVO>> getPatientPrescriptionList(PatientPrescriptionListREQ req) {
        // 分页处理
        PageHelper.startPage(req.getPageNum(), req.getPageSize());

        // 查询符合条件的处方列表
        List<PatientPrescriptionVO> prescriptionList = prescriptionMapper.selectPatientPrescriptions(req);

        // 处理数据：解密审核医生姓名 + 完善状态描述 + 处方类型描述
        prescriptionList.forEach(vo -> {
            // 1. 解密审核医生姓名（review_doctor_name 是加密字段，匹配 InquiryPrescriptionDO 实体）
//            if (StringUtils.isNotBlank(vo.getReviewDoctorName())) {
//                try {
//                    String decryptReviewDoctorName = KingNetUtils.getDecryptBase64CBC(vo.getReviewDoctorName());
//                    vo.setReviewDoctorName(decryptReviewDoctorName);
//                } catch (Exception e) {
//                    log.error("解密审核医生姓名失败，处方ID：{}", vo.getId(), e);
//                    vo.setReviewDoctorName("");
//                }
//            }

            // 2. 处方状态描述（匹配 InquiryPrescriptionDO 中的状态枚举）
            if (StringUtils.isNotBlank(vo.getStatus())) {
                switch (vo.getStatus()) {
                    case "2":
                        vo.setStatusDesc("已审核");
                        break;
                    default:
                        vo.setStatusDesc("未知状态");
                        break;
                }
            }

            // 3. 处方类型描述（匹配 InquiryPrescriptionDO 中的 prescriptType：1=西药，2=中药）
            if (vo.getPrescriptType() != null) {
                switch (vo.getPrescriptType()) {
                    case 1:
                        vo.setPrescriptTypeDesc("西药处方");
                        break;
                    case 2:
                        vo.setPrescriptTypeDesc("中药处方");
                        break;
                    default:
                        vo.setPrescriptTypeDesc("未知处方类型");
                        break;
                }
            }

            // 4. 西药字段默认值处理（匹配 InquiryPrescriptWesternMedicineDO 实体）
            if (CollectionUtil.isNotEmpty(vo.getWesternMedicines())) {
                vo.getWesternMedicines().forEach(westernMedicine -> {
                    // 剂量单位默认值
                    if (StringUtils.isBlank(westernMedicine.getDoseUnit())) {
                        westernMedicine.setDoseUnit("");
                    }
                    // 用药天数默认值
                    if (StringUtils.isBlank(westernMedicine.getDays())) {
                        westernMedicine.setDays("0");
                    }
                    // 单价默认值
                    if (westernMedicine.getUnitPrice() == null) {
                        westernMedicine.setUnitPrice(BigDecimal.ZERO);
                    }
                    // 总量默认值
                    if (westernMedicine.getQuantity() == null) {
                        westernMedicine.setQuantity(0);
                    }
                });
            }

            // 5. 中药字段默认值处理（匹配 InquiryPrescriptChineseMedicineDO 实体）
            if (CollectionUtil.isNotEmpty(vo.getChineseMedicines())) {
                vo.getChineseMedicines().forEach(chineseMedicine -> {
                    // 付数默认值
                    if (chineseMedicine.getQuantity() == null) {
                        chineseMedicine.setQuantity(0);
                    }
                    // 单价默认值
                    if (StringUtils.isBlank(chineseMedicine.getUnitPrice())) {
                        chineseMedicine.setUnitPrice("0");
                    }
                    // 煎法默认值
                    if (StringUtils.isBlank(chineseMedicine.getDecoction())) {
                        chineseMedicine.setDecoction("常规煎法");
                    }
                });
            }
        });

        PageInfo<PatientPrescriptionVO> pageInfo = new PageInfo<>(prescriptionList);
        return R.ok(prescriptionList, pageInfo.getTotal());
    }
}
